//
//  ZWFirstViewController.m
//  WanEr
//
//  Created by 高文萍 on 16/1/13.
//  Copyright © 2016年 ziwokeji. All rights reserved.
//

#import "ZWFirstViewController.h"
#import "ZWBaseCollecView.h"
#import "ZWFirstCollectionViewCell.h"
#import "ZWHeader.h"
#import "ZWFirstModel.h"
#import "ZWHeaderView.h"
#import "ZWTwiceViewController.h"
#import "ZWChouTiView.h"// 抽屉
#import "ZWChouTiTableViewCell.h"
@interface ZWFirstViewController ()<UICollectionViewDelegate,UICollectionViewDataSource, UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) ZWBaseCollecView *firstCollection;
@property (nonatomic, strong) NSMutableArray *firstArr;
@property (nonatomic, strong) UIImageView *headerImg;
@property (nonatomic, strong) ZWHeaderView *headerView;
// 抽屉
@property (nonatomic, strong) UITableView *chouTiTable;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) ZWChouTiView *chouTiView;
@property (nonatomic, strong) UIView *choutibackView;
@end

@implementation ZWFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headerView = (ZWHeaderView *)[[[NSBundle mainBundle]loadNibNamed:@"firstHeader" owner:self options:nil]lastObject] ;
    self.headerView.frame = CGRectMake(0, 0, WIDTH, 64);
    [self.view addSubview:self.headerView];
    [self.headerView.locationBut setTitle:@"厦门" forState:UIControlStateNormal];
    [self.headerView.locationBut setTitleColor:[UIColor colorWithWhite:0.041 alpha:1.000] forState:UIControlStateNormal];
    [self.headerView.locationBut addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    self.headerView.locationBut.tag = 100;
    [self.headerView.chouTiBut addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    self.headerView.chouTiBut.tag = 101;
    
    self.firstCollection = [[ZWBaseCollecView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49) flowLayoutItemSize:CGSizeMake((WIDTH - 14 * 2 - 30) / 2, (WIDTH - 14 * 2- 30) / 2 + 52) xSpace:20 ySpace:0 sectionInset:UIEdgeInsetsMake(20, 14, 15, 14) cell:[ZWFirstCollectionViewCell class] reuse:@"reuseCell"];
    self.firstCollection.delegate = self;
    self.firstCollection.dataSource = self;
    // 关掉显示垂直的进度条
    self.firstCollection.showsVerticalScrollIndicator = NO;
    // 关掉显示水平的进度条
    self.firstCollection.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.firstCollection];
    
    
    [self createData];
}

- (void)createData
{
    self.firstArr = [NSMutableArray array];
    NSString *str = @"厦门";
    [JXRAFNetWorkTool getNetWithURL:[[NSString stringWithFormat:@"http://app.ziwoxz.com/agi/district/list_city_sceneries/dname/%@",str] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] body:self resultKind:JXRJSON headerFile:nil success:^(id result) {
        
        self.firstArr = [ZWFirstModel baseModelByArr:result[@"data"][@"sceneries"] arr:self.firstArr];
        [self.firstCollection reloadData];
        
        
    } failure:^(NSError *error) {
        
    }];
}
#pragma -----抽屉--------------
- (void)createChouTi
{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    self.choutibackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.choutibackView.backgroundColor = [UIColor clearColor];
    [window addSubview:self.choutibackView];
    // 手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    [self.choutibackView addGestureRecognizer:tap];

    
    self.chouTiView = (ZWChouTiView *)[[[NSBundle mainBundle]loadNibNamed:@"ChouTi" owner:self options:nil] lastObject];
    self.chouTiView.frame = CGRectMake(- WIDTH / 4 * 3, 0, WIDTH / 4 * 3, HEIGHT);
    self.chouTiView.backgroundColor = [UIColor colorWithWhite:0.188 alpha:1.000];
    [window addSubview:self.chouTiView];
    
    self.chouTiTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 130, WIDTH / 4 * 3, HEIGHT - 130 - 55) style:UITableViewStylePlain];
    self.chouTiTable.delegate = self;
    self.chouTiTable.dataSource = self;
    self.chouTiTable.separatorStyle = NSUnderlineStyleNone;
    self.chouTiTable.backgroundColor = [UIColor colorWithWhite:0.188 alpha:1.000];
    [self.chouTiView addSubview:self.chouTiTable];
    self.arr = [NSMutableArray arrayWithObjects:@"基本信息",@"我的下载 ",@"我的收藏", nil];
    
    // 滑动手势
    UIPanGestureRecognizer *tapV = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapVVVClick:)];
    [self.chouTiView addGestureRecognizer:tapV];

}
#pragma ------抽屉收起手势---------
- (void)tapClick:(UITapGestureRecognizer *)tap
{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.chouTiView.frame = CGRectMake(-WIDTH / 4 * 3, 0, WIDTH / 4 * 3, HEIGHT);
        self.choutibackView.alpha = 0;
        
    }];
    
}
#pragma --滑动手势--
- (void)tapVVVClick:(UIPanGestureRecognizer *)tap
{
    
    UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)tap;
    CGPoint point = [gesture translationInView:self.chouTiView];
    CGPoint tablePoint = [gesture translationInView:self.firstCollection];
    NSLog(@"%f --- %f   --- %f",point.x,point.y,self.chouTiView.frame.origin.x);
    if (self.chouTiView.frame.origin.x + point.x> 0) {// 从x坐标开始
        
        return ;
        
    }else{
        
        self.chouTiView.transform = CGAffineTransformTranslate(self.chouTiView.transform, point.x, self.chouTiView.frame.origin.y);
        
        if (tap.state == UIGestureRecognizerStateRecognized) {
            NSLog(@"结束");
            if (self.chouTiView.frame.origin.x + point.x < 220) {
                
                [self setViewHidden:YES view:self.chouTiView];
                
            }else{
                
                [self setViewHidden:NO view:self.chouTiView];
            }
        }
    }
    
    [tap setTranslation:CGPointZero inView:self.chouTiView];
}
#pragma mark 抽屉弹出消失时调用的方法
- (void)setViewHidden:(BOOL)hidden view:(UIView *)view{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.15];
    if (hidden) {
        [view setFrame:CGRectMake(-WIDTH / 4 * 3, 0, WIDTH / 4 * 3 , HEIGHT)];
        
        // firstView 消失时 透明度变为零
        if (view == self.chouTiView){
            self.choutibackView.alpha = 0;
        }
    }else{
        
        [view setFrame:CGRectMake(0, 0,WIDTH / 4 * 3, HEIGHT)];

    }
    [UIView commitAnimations];
}
// 抽屉按钮
- (void)click:(UIButton *)button
{
    if (button.tag == 100) {
        NSLog(@"选择城市");
    }else if (button.tag == 101){
        NSLog(@"抽屉");
        if (!self.choutibackView) {
            [self createChouTi];
        }
        [UIView animateWithDuration:0.5 animations:^{
            self.chouTiView.frame = CGRectMake(0, 0, WIDTH / 4 * 3, HEIGHT);
            
        }];

    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseChou = @"chouReuse";
    UITableViewCell *cellChou = [tableView dequeueReusableCellWithIdentifier:reuseChou];
    if (!cellChou) {
        cellChou = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseChou];
    }
    
    cellChou.textLabel.text = self.arr[indexPath.row];
    cellChou.textLabel.textColor = [UIColor whiteColor];
    cellChou.backgroundColor = [UIColor colorWithWhite:0.188 alpha:1.000];
    
//    if (indexPath.row == 0) {
//        cellChou.imageView.image = [UIImage imageNamed:@"基本信息高亮"];
//    }else if (indexPath.row == 1){
//        cellChou.imageView.image = [UIImage imageNamed:@"下载高亮"];
//    }else if (indexPath.row == 2){
//        cellChou.imageView.image = [UIImage imageNamed:@"收藏高亮"];
//    }
    return cellChou;
}

#pragma ---------主页--------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.firstArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZWFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCell" forIndexPath:indexPath];
    ZWFirstModel *model = (ZWFirstModel *)self.firstArr[indexPath.row];
    [cell.firstImg sd_setImageWithURL:[NSURL URLWithString:model.image_url]placeholderImage:[UIImage imageNamed:@"首页占位图"]];
    cell.cityLabel.text = model.scenerie_name;
    cell.describeLabel.text = model.comment;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZWTwiceViewController *twiceVC = [[ZWTwiceViewController alloc]init];
    ZWFirstModel *model = (ZWFirstModel *)self.firstArr[indexPath.row ];
    twiceVC.cityStr = model.scenerie_name;
    twiceVC.cityImg = model.image_url;
    twiceVC.uId = model.equipId;
    [self.navigationController pushViewController:twiceVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
